import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

class Student {
    private String name;
    private String studentId;
    private String course;
    private double grade;

    public Student(String name, String studentId, String course, double grade) {
        this.name = name;
        this.studentId = studentId;
        this.course = course;
        this.grade = grade;
    }

    // Getters and setters
    public String getName() { return name; }
    public String getStudentId() { return studentId; }
    public String getCourse() { return course; }
    public double getGrade() { return grade; }

    public void setName(String name) { this.name = name; }
    public void setStudentId(String studentId) { this.studentId = studentId; }
    public void setCourse(String course) { this.course = course; }
    public void setGrade(double grade) { this.grade = grade; }
}

class GradeManager {
    private List<Student> students;
    private String dataFile;
    private Gson gson;

    public GradeManager(String dataFile) {
        this.dataFile = dataFile;
        this.students = new ArrayList<>();
        this.gson = new GsonBuilder().setPrettyPrinting().create();
        loadData();
    }

    public void loadData() {
        File file = new File(dataFile);
        if (file.exists()) {
            try {
                String content = new String(Files.readAllBytes(Paths.get(dataFile)));
                students = gson.fromJson(content, new TypeToken<List<Student>>(){}.getType());
                if (students == null) {
                    students = new ArrayList<>();
                }
            } catch (IOException e) {
                System.out.println("加载数据时出错: " + e.getMessage());
                students = new ArrayList<>();
            }
        } else {
            students = new ArrayList<>();
        }
    }

    public void saveData() {
        try (FileWriter writer = new FileWriter(dataFile)) {
            gson.toJson(students, writer);
        } catch (IOException e) {
            System.out.println("保存数据时出错: " + e.getMessage());
        }
    }

    public void addStudent(String name, String studentId, String course, double grade) {
        // 检查学号是否重复
        for (Student student : students) {
            if (student.getStudentId().equals(studentId) && 
                student.getCourse().equals(course)) {
                System.out.println("错误: 该学生在该课程的成绩已存在!");
                return;
            }
        }

        // 检查成绩范围
        if (grade < 0 || grade > 100) {
            System.out.println("错误: 成绩必须在0-100分之间!");
            return;
        }

        students.add(new Student(name, studentId, course, grade));
        System.out.println("成绩添加成功!");
        saveData();
    }

    public void queryByStudentId(String studentId) {
        boolean found = false;
        for (Student student : students) {
            if (student.getStudentId().equals(studentId)) {
                System.out.printf("姓名: %s, 学号: %s, 课程: %s, 成绩: %.1f%n",
                    student.getName(), student.getStudentId(),
                    student.getCourse(), student.getGrade());
                found = true;
            }
        }
        if (!found) {
            System.out.println("未找到该学生的成绩记录!");
        }
    }

    public void queryByCourse(String course) {
        boolean found = false;
        for (Student student : students) {
            if (student.getCourse().equals(course)) {
                System.out.printf("姓名: %s, 学号: %s, 课程: %s, 成绩: %.1f%n",
                    student.getName(), student.getStudentId(),
                    student.getCourse(), student.getGrade());
                found = true;
            }
        }
        if (!found) {
            System.out.println("未找到该课程的成绩记录!");
        }
    }

    public void queryByName(String name) {
        boolean found = false;
        for (Student student : students) {
            if (student.getName().equals(name)) {
                System.out.printf("姓名: %s, 学号: %s, 课程: %s, 成绩: %.1f%n",
                    student.getName(), student.getStudentId(),
                    student.getCourse(), student.getGrade());
                found = true;
            }
        }
        if (!found) {
            System.out.println("未找到该学生的成绩记录!");
        }
    }

    public void statisticsByCourse(String course) {
        List<Student> courseStudents = new ArrayList<>();
        for (Student student : students) {
            if (student.getCourse().equals(course)) {
                courseStudents.add(student);
            }
        }

        if (courseStudents.isEmpty()) {
            System.out.println("未找到该课程的成绩记录!");
            return;
        }

        double sum = 0;
        double max = Double.MIN_VALUE;
        double min = Double.MAX_VALUE;

        for (Student student : courseStudents) {
            double grade = student.getGrade();
            sum += grade;
            if (grade > max) max = grade;
            if (grade < min) min = grade;
        }

        double average = sum / courseStudents.size();

        System.out.printf("课程: %s%n", course);
        System.out.printf("平均分: %.2f%n", average);
        System.out.printf("最高分: %.1f%n", max);
        System.out.printf("最低分: %.1f%n", min);
    }
}

public class Main {
    public static void main(String[] args) {
        GradeManager manager = new GradeManager("grades.json");
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("\n===== 学生成绩管理系统 =====");
            System.out.println("1. 录入学生成绩");
            System.out.println("2. 按学号查询成绩");
            System.out.println("3. 按姓名查询成绩");
            System.out.println("4. 按课程查询成绩");
            System.out.println("5. 统计课程成绩");
            System.out.println("6. 退出系统");
            System.out.print("请选择操作(1-6): ");

            int choice = 0;
            try {
                choice = Integer.parseInt(scanner.nextLine());
            } catch (NumberFormatException e) {
                System.out.println("错误: 请输入有效的数字!");
                continue;
            }

            switch (choice) {
                case 1:
                    System.out.print("请输入学生姓名: ");
                    String name = scanner.nextLine();
                    System.out.print("请输入学号: ");
                    String studentId = scanner.nextLine();
                    System.out.print("请输入课程名称: ");
                    String course = scanner.nextLine();
                    double grade = 0;
                    while (true) {
                        System.out.print("请输入成绩(0-100): ");
                        try {
                            grade = Double.parseDouble(scanner.nextLine());
                            if (grade >= 0 && grade <= 100) break;
                            System.out.println("错误: 成绩必须在0-100之间!");
                        } catch (NumberFormatException e) {
                            System.out.println("错误: 请输入有效的数字!");
                        }
                    }
                    manager.addStudent(name, studentId, course, grade);
                    break;
                case 2:
                    System.out.print("请输入要查询的学号: ");
                    manager.queryByStudentId(scanner.nextLine());
                    break;
                case 3:
                    System.out.print("请输入要查询的姓名: ");
                    manager.queryByName(scanner.nextLine());
                    break;
                case 4:
                    System.out.print("请输入要查询的课程: ");
                    manager.queryByCourse(scanner.nextLine());
                    break;
                case 5:
                    System.out.print("请输入要统计的课程: ");
                    manager.statisticsByCourse(scanner.nextLine());
                    break;
                case 6:
                    System.out.println("感谢使用学生成绩管理系统，再见!");
                    scanner.close();
                    return;
                default:
                    System.out.println("错误: 无效的选择，请输入1-6之间的数字!");
            }
        }
    }
}    